package com.leo.blog.web.blogadmin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.leo.blog.login.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.leo.blog.annotatiton.SystemLog;
import com.leo.blog.dto.BlogTypeExecution;
import com.leo.blog.entity.BlogType;
import com.leo.blog.entity.Bloger;
import com.leo.blog.service.BlogTypeService;
import com.leo.blog.util.ConstantUtil;
import com.leo.blog.util.HttpServletRequestUtil;

@Controller
@RequestMapping(value = "/blogadmin")
public class BlogTypeManagementController {
	//加入运行日志
	Logger logger = LoggerFactory.getLogger(BlogTypeManagementController.class);
	@Autowired
	private BlogTypeService blogTypeService;
	
	@RequestMapping(value = "/blogtypelist",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> blogTypeList(){
		logger.info("===start===");
		long startTime = System.currentTimeMillis();//获取当前时间
		Map<String, Object> modelMap = new HashMap<>();
		List<BlogType> blogTypeList = new ArrayList<>();
		try {
			blogTypeList = blogTypeService.getBlogTypeList();//转换成数组,前台js可以调用map方法遍历
			//放进modelMap,传给前台
			modelMap.put("typeList", blogTypeList);
			modelMap.put("total", blogTypeList.size());//总数
			modelMap.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			//数据获取失败
			modelMap.put("success", false);
			modelMap.put("errMsg", e.toString());
		}
		long endTime = System.currentTimeMillis();
		logger.debug("costTime:[{}]", endTime - startTime);//运行所用时间
		logger.info("===end===");
		return modelMap;
	}
	
	@RequestMapping(value = "/getblogtypebyid")
	@ResponseBody
	public Map<String, Object> getBlogTypeById(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int blogTypeId = HttpServletRequestUtil.getInt(request, "blogTypeId");
		if(blogTypeId > 0) {
			BlogTypeExecution be = blogTypeService.getBlogTypeById(blogTypeId);
			modelMap.put("blogType", be.getBlogType());
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询失败");
		}
		return modelMap;
	}
	
	@RequestMapping(value = "/addblogtype")
	@ResponseBody
	@Transactional
	public Map<String, Object> addBlogType(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String blogTypeName = HttpServletRequestUtil.getString(request, "blogTypeName");
		
		Bloger bloger = SecurityUtils.getBloger();
		
		if(bloger.getHasPermission() == 1) {
			BlogType blogType = new BlogType();
			blogType.setBlogTypeName(blogTypeName);
			int effectedNum = blogTypeService.addBlogType("添加了博客类型", blogType);
			if(effectedNum > 0) {
				modelMap.put("success", true);
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "添加失败");
			}	
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "无权限操作");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/editblogtype")
	@ResponseBody
	public Map<String, Object> editBlogType(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		BlogType blogType = new BlogType();
		//获取前台取到的值
		String blogTypeName = HttpServletRequestUtil.getString(request, "blogTypeName");
		int blogTypeNum = HttpServletRequestUtil.getInt(request, "blogTypeNum");
		int blogTypeId = HttpServletRequestUtil.getInt(request, "blogTypeId");

		Bloger bloger = SecurityUtils.getBloger();
		
		if(bloger.getHasPermission() == 1) {
			blogType.setBlogTypeId(blogTypeId);
			if(blogTypeId <= 0) {
				modelMap.put("success", false);
				modelMap.put("msg", "更新失败");
			}else {
				if(blogTypeName != null) {
					blogType.setBlogTypeName(blogTypeName);
				}
				if(blogTypeNum > 0) {
					blogType.setBlogTypeNum(blogTypeNum);
				}	
				int effectedNum = blogTypeService.editBlogType("更新了博客类型", blogType);
				if(effectedNum > 0) {
					modelMap.put("success", true);
				}else {
					modelMap.put("success", false);
					modelMap.put("msg", "更新失败");
				}
			}	
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "无权限操作");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/deleteblogtype")
	@ResponseBody
	public Map<String, Object> deleteBlogType(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int blogTypeId = HttpServletRequestUtil.getInt(request, "blogTypeId");
		
		Bloger bloger = (Bloger)request.getSession().getAttribute("bloger");
		
		if(bloger.getHasPermission() == 1) {
			if(blogTypeId > 0 ) {
				int cunzai = blogTypeService.getBlogByBlogTypeCount(blogTypeId);
				if(cunzai > 0) {
					modelMap.put("status", 2);
					modelMap.put("msg", "删除失败");
				}else {
					int effectedNum = blogTypeService.deleteBlogType("删除了博客类型", blogTypeId);
					if(effectedNum > 0) {
						modelMap.put("success", true);
					}else {
						modelMap.put("success", false);
						modelMap.put("msg", "删除失败");
					}
				}			
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "删除失败");
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "无权限操作");
		}
		
		return modelMap;
	}	
	
}
